package com.example.controller;

import com.example.bean.ArticleBean;
import com.example.service.ArticleService;
import com.example.util.Ret;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@ResponseBody
@RequestMapping("/article")
public class ArticleController {
	
	@Autowired
	ArticleService articleService;
    
	/**
     * 保存文章
     * 
     * @author: zhangwei
     * @date: 2021年1月15日
     */
    @RequestMapping("/save")
    public Ret save(ArticleBean articleBean) {
    	Ret ret;
    	try {
    		if(!articleService.save(articleBean)) throw new Exception();
			ret = Ret.ok("msg", "发布文章成功！");
		} catch (Exception e) {
			e.printStackTrace();
			ret = Ret.fail("msg", "发布文章出错，请刷新后重试！");
		}
    	return ret;
    }
    
    /**
     * 获取文章
     * 
     * @author: zhangwei
     * @date: 2021年1月15日
     */
    @RequestMapping("/list")
    public Ret findArticle(String query) {
    	Ret ret;
    	try {
			ret = Ret.ok("data", articleService.findArticle(query));
		} catch (Exception e) {
			e.printStackTrace();
			ret = Ret.fail("msg", "获取文章出错，请刷新后重试！");
		}
    	return ret;
    }
}







